package cn.lingyangwl.agile.pay.constant;

import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
 * @author shenguangyang
 */
@Getter
@AllArgsConstructor
public enum IfCodeEnum {
    ALIPAY("ali_pay", "支付宝官方支付"),
    WXPAY("wx_pay", "微信官方支付"),
    MERGE_PAY("merge_pay", "聚合支付"),

    ;
    private final String code;
    private final String desc;

    public static IfCodeEnum ofByCode(String ifCode) {
        if (StringUtils.isEmpty(ifCode)) {
            throw new BizException("支付接口不能为空");
        }
        return Arrays.stream(IfCodeEnum.values())
                .filter(e -> e.getCode().equalsIgnoreCase(ifCode))
                .findFirst().orElseThrow(() -> new BizException("支付接口 {} 不存在", ifCode));
    }

    public static IfCodeEnum ofByCode(String ifCode, boolean isThrow) {
        if (isThrow) {
            return ofByCode(ifCode);
        }

        if (StringUtils.isEmpty(ifCode)) {
            return null;
        }
        return Arrays.stream(IfCodeEnum.values())
                .filter(e -> e.getCode().equalsIgnoreCase(ifCode))
                .findFirst().orElse(null);
    }
}
